﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Threading;
using System.Globalization;
using System.Resources;
using DataAccess.BusinessObject;
using DataAccess.DataObject;
using System.Collections.Generic;

namespace EnmineKaitlin.MasterPage
{
    public partial class SubPage : System.Web.UI.MasterPage
    {
        ResourceManager rm;
        CultureInfo ci;
        public int CatID = 0;
        public int PostID = 0;
        BCategory _BCategory = new BCategory();
        BPost _BPost = new BPost();
        public Common.PageBase _PageBase = new EnmineKaitlin.Common.PageBase();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Request.Params["cat"] != null)
                    {
                        string strcat = Request.Params["cat"].ToString();
                        strcat = strcat.Substring(strcat.LastIndexOf("-") + 1);
                        CatID = int.Parse(strcat);
                    }
                    if (CatID > 0)
                    {
                        if (Request.Params["p"] != null && Request.Params["p"].ToString()!="")
                        {
                            string strpost = Request.Params["p"].ToString();
                            strpost = strpost.Substring(strpost.LastIndexOf("-") + 1);
                            PostID = int.Parse(strpost);
                        }
                        BindPage(CatID, PostID);
                    }
                    rptBannerSubPage.DataSource = _BPost.Get(Common.PageBase.BannerTrai_PostID);
                    rptBannerSubPage.DataBind();
                }
                catch (Exception ex)
                {
                    Response.Redirect("~/home.aspx");
                }                                
            }
        }        
        protected void BindPage(int CatID, int PostID)
        {
            pnlnavleft.Visible = true;
            IList<OCategory> lstCategory = _BCategory.Get(CatID, "vn");
            IList<OCategory> lstCatParent;
            int CatParent = lstCategory[0].CatParent;
            IList<OCategory> lstCatChild = _BCategory.Get(0, CatID, "vn");
            IList<OPost> lstPost;
            //Load ảnh đại diện của chuyên mục
            if (CatParent > 0)
            {                
                lstCatParent = _BCategory.Get(lstCategory[0].CatParent, "vn");
                lblCatName.Text = lstCatParent[0].CatName;
                if(lstCatParent[0].FeaturedImage!=""){
                    imgBanner.Visible = true;
                    imgBanner.ImageUrl = lstCatParent[0].FeaturedImage;
                    imgBanner.DataBind();
                }
                //Bind sidebar leFt
                rptListCat.DataSource = _BCategory.Get(0, lstCatParent[0].CatID, "vn");
                rptListCat.DataBind();                
            }
            else
            {
                lblCatName.Text = lstCategory[0].CatName;
                if (lstCategory[0].FeaturedImage != "")
                {
                    imgBanner.Visible = true;
                    imgBanner.ImageUrl = lstCategory[0].FeaturedImage;
                    imgBanner.DataBind();
                }
                if (lstCatChild.Count > 0)
                {
                    rptListCat.DataSource = lstCatChild;
                    rptListCat.DataBind();                    
                }
                else
                {
                    rptListLeft.DataSource = _BPost.Get(CatID, 100);
                    rptListLeft.DataBind();                    
                }
            }            
        }        
    }
}
